//! component_factory.rs 单元测试
//! 
//! 测试组件工厂模式的实现，验证：
//! 1. 默认组件工厂的功能
//! 2. 扩展组件工厂的特性
//! 3. 工厂注册表的管理
//! 4. 组件创建的正确性
//! 5. 错误处理机制

use redis_rs2::server::component_factory::*;
use redis_rs2::server::enhanced_traits::*;
use redis_rs2::storage::RedisServerMode;
use tokio_test;

// 复用Mock实现
use super::enhanced_traits_test::{MockServerConfig};

#[cfg(test)]
mod default_component_factory_tests {
    use super::*;
    
    #[tokio::test]
    async fn test_default_factory_creation() {
        let factory = DefaultComponentFactory;
        let supported_modes = factory.supported_modes();
        
        assert_eq!(supported_modes.len(), 2);
        assert!(supported_modes.contains(&RedisServerMode::Standalone));
        assert!(supported_modes.contains(&RedisServerMode::Cluster));
    }
    
    #[tokio::test]
    async fn test_create_redis_service_standalone() {
        let factory = DefaultComponentFactory;
        let config = MockServerConfig {
            mode: RedisServerMode::Standalone,
            bind_address: "127.0.0.1:6379".to_string(),
            should_validate: true,
        };
        
        let result = factory.create_redis_service(&config).await;
        assert!(result.is_ok());
        
        let service = result.unwrap();
        assert_eq!(service.service_name(), "RedisService");
    }
    
    #[tokio::test]
    async fn test_create_cluster_service_for_cluster_mode() {
        let factory = DefaultComponentFactory;
        let config = MockServerConfig {
            mode: RedisServerMode::Cluster,
            bind_address: "127.0.0.1:7001".to_string(),
            should_validate: true,
        };
        
        let result = factory.create_cluster_service(&config).await;
        assert!(result.is_ok());
        
        let service_option = result.unwrap();
        assert!(service_option.is_some());
        
        let service = service_option.unwrap();
        assert_eq!(service.service_name(), "ClusterService");
    }
    
    #[tokio::test]
    async fn test_no_cluster_service_for_standalone_mode() {
        let factory = DefaultComponentFactory;
        let config = MockServerConfig {
            mode: RedisServerMode::Standalone,
            bind_address: "127.0.0.1:6379".to_string(),
            should_validate: true,
        };
        
        let result = factory.create_cluster_service(&config).await;
        assert!(result.is_ok());
        
        let service_option = result.unwrap();
        assert!(service_option.is_none());
    }
}

#[cfg(test)]
mod factory_registry_tests {
    use super::*;
    
    #[test]
    fn test_factory_registry_creation() {
        let registry = ComponentFactoryRegistry::new();
        let factory_names = registry.factory_names();
        
        // 应该有默认的工厂
        assert!(factory_names.contains(&"default".to_string()));
        assert!(factory_names.contains(&"extended".to_string()));
    }
    
    #[test]
    fn test_factory_registry_get_default() {
        let registry = ComponentFactoryRegistry::new();
        let default_factory = registry.default_factory();
        
        let supported_modes = default_factory.supported_modes();
        assert!(!supported_modes.is_empty());
    }
    
    #[test]
    fn test_factory_registry_custom_registration() {
        let mut registry = ComponentFactoryRegistry::new();
        let custom_factory = std::sync::Arc::new(DefaultComponentFactory);
        
        registry.register_factory("custom".to_string(), custom_factory.clone());
        
        let retrieved_factory = registry.get_factory("custom");
        assert!(retrieved_factory.is_some());
        
        let factory_names = registry.factory_names();
        assert!(factory_names.contains(&"custom".to_string()));
    }
    
    #[test]
    fn test_factory_registry_unregister() {
        let mut registry = ComponentFactoryRegistry::new();
        let custom_factory = std::sync::Arc::new(DefaultComponentFactory);
        
        registry.register_factory("temp".to_string(), custom_factory);
        assert!(registry.get_factory("temp").is_some());
        
        let removed = registry.unregister_factory("temp");
        assert!(removed.is_some());
        assert!(registry.get_factory("temp").is_none());
    }
    
    #[test]
    fn test_factory_registry_cannot_remove_default() {
        let mut registry = ComponentFactoryRegistry::new();
        
        let removed = registry.unregister_factory("default");
        assert!(removed.is_none());
        
        // 默认工厂应该还在
        assert!(registry.get_factory("default").is_some());
    }
}

#[cfg(test)]
mod error_handling_tests {
    use super::*;
    
    #[tokio::test]
    async fn test_invalid_config_validation() {
        let factory = DefaultComponentFactory;
        let config = MockServerConfig {
            mode: RedisServerMode::Standalone,
            bind_address: "127.0.0.1:6379".to_string(),
            should_validate: false, // 配置验证失败
        };
        
        let result = factory.create_redis_service(&config).await;
        assert!(result.is_err());
    }
}